#  Gmof - A meta-object facility library based on OMG's EMOF 
#
#  Copyright (C) 2007 Peter Hagg <peter.hagg@iki.fi>
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2.1 of the License, or (at your option) any later version.
#
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
from Element import Element
from String import String
from Class import Class
from NullPointerException import NullPointerException
from Property import Property

class Factory(Element):
    def __init__(self, package):
        super(Factory, self).__init__()
        
        self.package = package
        
    def createFromString(self, dataType, string):
        instance = None
        
        if (dataType.__class__ == String):
            instance = String(dataType.name, string)
        elif (dataType.__class__ == Boolean):
            instance = Boolean(dataType.name, string)
        elif (dataType.__class__ == Integer):
            instance = Integer(dataType.name, string)
        else:
            return None
        
        instance.package = self.package
        instance.metaClass = dataType

        return instance
    
    def convertToString(self, dataType, object):
        return ""
    
    def create(self, metaClass):
        
        # Check validity of meta class
        if (metaClass == None):
            raise NullPointerException("metaClass == None")

        if (metaClass.package != self.package):
            raise IllegalArgumentException("metaClass.package != Factory.package")
        
        if (metaClass.isAbstract == True):
            return None
        
        # Set basic properties
        instance = Class(metaClass.name)
        instance.metaClass = metaClass
        instance.package = self.package

        # Add properties to class instance
        for property in metaClass.ownedAttribute:
            # Create an instance of the attribute type and set its value
            # to the default value of the property
            type = self.createFromString(property.type, property.default)
            type.package = self.package
            
            # Create an instance of the attribute
            attribute = Property(property.name, type)
            attribute.isOrdered = property.isOrdered
            attribute.isUnique = property.isUnique
            attribute.lower = property.lower
            attribute.upper = property.upper
            attribute.isReadOnly = property.isReadOnly
            attribute.default = property.default
            attribute.isComposite = property.isComposite
            attribute.isDerived = property.isDerived
            attribute.isID = property.isID
            attribute.opposite = property.opposite
            attribute.klass = instance
            
            # Add the attribute to the class instance
            instance.ownedAttribute.append(attribute)
        
        return instance
